package com.tbit.uqbike.webmanager.controller;

import com.tbit.uqbike.object.pojo.vo.AdopterAccountUserVO;
import com.tbit.uqbike.object.pojo.vo.AdopterMachineVO;
import com.tbit.uqbike.object.pojo.vo.AdopterOperatingStat;
import com.tbit.uqbike.object.pojo.vo.AdopterOperatingStatVO;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.common.entity.view.PageResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.AdopterMsgService;
import com.tbit.uqbike.webmanager.util.excel.ExcelUtil;
import com.tbit.utils.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 领养人数据接口
 *
 * @Author:BUGTian
 * @DATE: 2022/9/27
 */
@RestController
@RequestMapping("/adopterMsg")
public class AdopterMsgController {

    @Autowired
    private AdopterMsgService adopterMsgService;

    /**
     * 运营统计详情/图表
     *
     * @param adAccountId          区域id
     * @param startTime            开始时间
     * @param endTime              结束时间
     * @param regionIds            分区id
     * @param adopterAccountUserId 领养人账号id
     * @return
     */
    @GetMapping("/operatingStat")
    public Result<AdopterOperatingStatVO> adopterOperatingStat(HttpServletRequest request, @NotNull Integer adAccountId, @NotNull Date startTime, @NotNull Date endTime, Integer adopterAccountUserId) {
        Assert.notNulls("缺少必要参数", adAccountId, startTime, endTime);
        LoginInfo login = BaseController.getLogin(request);
        return Result.success(adopterMsgService.adopterOperatingStat(login.getAccountUserId(), adAccountId, startTime, endTime, adopterAccountUserId));
    }

    /**
     * 运营统计详情/图表 导出
     *
     * @param adAccountId          区域id
     * @param startTime            开始时间
     * @param endTime              结束时间
     * @param adopterAccountUserId 领养人账号id
     * @return
     */
    @GetMapping("/exportOperatingStat")
    public Result exportAdopterOperatingStat(HttpServletRequest request, @NotNull Integer adAccountId, @NotNull Date startTime, @NotNull Date endTime, Integer adopterAccountUserId) {
        Assert.notNulls("缺少必要参数", adAccountId, startTime, endTime);
        LoginInfo login = BaseController.getLogin(request);
        AdopterOperatingStatVO adopterOperatingStatVO = adopterMsgService.adopterOperatingStat(login.getAccountUserId(), adAccountId, startTime, endTime, adopterAccountUserId);
        List<AdopterOperatingStat> adopterOperatingStatList = adopterOperatingStatVO.getAdopterOperatingStatList();
        ExcelUtil<AdopterOperatingStat> excelUtil = new ExcelUtil<>(AdopterOperatingStat.class);
        return Result.success(excelUtil.exportExcel(adopterOperatingStatList, "运营统计详情").getData());
    }

    /**
     * 查询领养人车辆列表
     *
     * @param request
     * @param page                 当前页码
     * @param size                 每页显示的行数
     * @param adAccountId          区域id
     * @param machineNO            车辆编号
     * @param state                车辆状态
     * @param adopterAccountUserId 领养人账号id
     * @return
     */
    @GetMapping("/queryAdopterMachine")
    public Result<PageResult<AdopterMachineVO>> queryAdopterMachine(HttpServletRequest request, Date startTime, Date endTime,  @NotNull Integer page, @NotNull Integer size, Integer adAccountId, String machineNO, Integer state, Integer adopterAccountUserId) {
        Assert.notNulls("分页参数有误", page, size);
        LoginInfo login = BaseController.getLogin(request);
        Integer accountUserId = login.getAccountUserId();
        if (adopterAccountUserId != null) {
            accountUserId = adopterAccountUserId;
        }
        return adopterMsgService.queryAdopterMachine(page, size, accountUserId, adAccountId, machineNO, state, startTime, endTime);
    }

    /**
     * 领养人车辆列表导出
     * @return result
     */
    @GetMapping("/exportAdopter")
    public Result<Object> exportAdopter(Date startTime, Date endTime, Integer adAccountId, String machineNO, Integer state, Integer adopterAccountUserId, HttpServletRequest request) {
        if (Objects.isNull(startTime) || Objects.isNull(endTime)){
            return Result.error("时间不能为空");
        }
        LoginInfo login = BaseController.getLogin(request);
        Integer accountUserId = login.getAccountUserId();
        if (adopterAccountUserId != null) {
            accountUserId = adopterAccountUserId;
        }
        List<AdopterMachineVO> list = adopterMsgService.queryAdopterMachine(1, 65535, accountUserId, adAccountId, machineNO, state, startTime, endTime).getData().getList();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String fileName = String.format("%s~%s 领养人车辆列表", sdf.format(startTime), sdf.format(endTime));
        ExcelUtil<AdopterMachineVO> excelUtil = new ExcelUtil<>(AdopterMachineVO.class);
        JsonResult result = excelUtil.exportExcel(list, fileName);
        return Result.success(result.getData());
    }


    /**
     * 查询领养人账号列表
     *
     * @param accountId 品牌id
     * @return
     */
    @GetMapping("/queryAdopterAccountUser")
    public Result<List<AdopterAccountUserVO>> queryAdopterAccountUser(@NotNull Integer accountId) {
        return Result.success(adopterMsgService.queryAdopterAccountUser(accountId));
    }

}
